﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DCRF.Common.Primitive;

namespace DCRF.BaseBlocks.Log
{
    public partial class ctlLog : UserControl
    {
        private List<LogType> logTypes = new List<LogType>();

        public ctlLog()
        {
            InitializeComponent();
        }

        private void cmdClear_Click(object sender, EventArgs e)
        {
            lstLog.Items.Clear();
        }

        private void chkAutoScroll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAutoScroll.Checked)
            {
                lstLog.SelectedIndex = lstLog.Items.Count - 1;
                lstLog.SelectedIndex = -1;
            }
        }

        public void Log(LogType type, string text)
        {
            if (chkLog.Checked)
            {
                int idx = logTypes.IndexOf(type);
                bool isEnabled = chkFilter.GetItemChecked(idx);

                if (isEnabled)
                {
                    lstLog.Items.Add(text);

                    if (chkAutoScroll.Checked)
                    {
                        lstLog.SelectedIndex = lstLog.Items.Count - 1;
                        lstLog.SelectedIndex = -1;
                    }
                }
            }
        }

        private void ctlLog_Load(object sender, EventArgs e)
        {
            Array values = Enum.GetValues(typeof(LogType));
            string[] names = Enum.GetNames(typeof(LogType));

            for (int i = 0; i < names.Length; i++)
            {
                LogType value = (LogType)values.GetValue(i);

                logTypes.Add(value);

                chkFilter.Items.Add(names[i], true);
            }            
        }

    }
}
